import { Toast } from "antd-mobile";

const initialState = {
  narr: [], //查找
  arr: [], //渲染数据
  kg: false, //全选开关
};

export default (state = initialState, { type, payload }: any) => {
  let newState = JSON.parse(JSON.stringify(state));
  switch (type) {
    //获取数据
    case "getlist":
      newState.arr = payload;
      newState.narr = payload;
      return newState;
    //添加数据
    case "updateobj":
      newState.arr.push(payload);
      newState.narr = newState.arr;
      return newState;
    //删除数据
    case "delete":
      newState.arr.splice(payload, 1);
      newState.narr = newState.arr;
      return newState;
    //查找数据
    case "fillter":
      newState.arr = newState.narr.filter((item: any) => {
        return item.xiaoqu.includes(payload);
      });
      return newState;
    //改变审核状态
    case "checkedchange":
      newState.arr.forEach((item: any, index: number) => {
        if (item.id == payload.id) {
          item.checkedtwo = !item.checkedtwo;
          Toast.show("状态改变");
        }
      });
      newState.narr = newState.arr;
      return newState;
    //选中框状态
    case "Checkboxs":
      newState.kg = payload.target.checked;
      newState.arr.forEach((item: any) => {
        return (item.checked = payload.target.checked);
      });
      newState.narr = newState.arr;
      return newState;
    //选中删除
    case "delAll":
      let crr: any[] = [];
      newState.arr.forEach((item: any) => {
        if (item.checked === false) {
          crr.push(item);
        }
        newState.arr = crr;
        newState.narr = crr;
      });
      return newState;
    //反选
    case "check":
      newState.arr.forEach((item: any) => {
        if (item.id === payload.id) {
          item.checked = !item.checked;
          //每一个都符合条件以后返回真  every
          let kgMc = newState.arr.every((item: any) => {
            return item.checked;
          });
          newState.kg = kgMc;
        }
      });
      return newState;
    //下拉筛选
    case "onChange":
      if (payload == "启用") {
        newState.arr = newState.narr.filter((item: any) => {
          return item.checkedtwo == true;
        });
      } else if (payload == "待审") {
        newState.arr = newState.narr.filter((item: any) => {
          return item.checkedtwo == false;
        });
      } else {
        newState.arr = newState.narr;
      }
      return newState;
    //编辑
    case "upListgd":
      newState.arr.forEach((item: any, index: number) => {
        if (item.id == payload.id) {
          //回显
          newState.arr[index] = payload;
        }
      });
      newState.narr = newState.arr;
      return newState;
    default:
      return newState;
  }
};
